package com.atguigu.guli.service.oss.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.atguigu.guli.service.oss.service.FileService;
import com.atguigu.guli.service.oss.util.OssProperties;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {
    @Autowired
    OssProperties ossProperties;

    @Override

    public String upload(InputStream inputStream, String module, String originalFilename) {
//获取连接属性
        String bucketName = ossProperties.getBucketName();
        String endpoint = ossProperties.getEndpoint();
        String accessKeyId = ossProperties.getKeyId();
        String accessKeySecret = ossProperties.getKeySecret();
        //当前时间
        String s = new DateTime().toString("/yyyy/MM/dd/");
        String s1 = UUID.randomUUID().toString();
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        String key = module + s + s1+substring;

// 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// 上传文件流。
        ossClient.putObject(bucketName, key, inputStream);

// 关闭OSSClient。
        ossClient.shutdown();
        //返回url地址
        return "https://" + bucketName + "." + endpoint + "/" + key;
    }
}
